package v1

import (
	"RoccBlog/global"
	"RoccBlog/model"
	"RoccBlog/util"
)

type RouterRepository struct {
}

func (r *RouterRepository) Create(data model.Router) error {
	return global.DB.Create(&data).Error
}

func (r *RouterRepository) Delete(id int) error {
	return global.DB.Delete(&model.Router{}, id).Error
}

func (r *RouterRepository) Update(data model.Router) error {
	return global.DB.Model(&data).Updates(&data).Error
}

func (r *RouterRepository) Select() (any, error) {
	db := global.DB.Model(&model.Router{})

	var list []model.Router
	var count int64
	var err error
	err = db.Count(&count).Error
	if err != nil {
		return nil, err
	}

	db.Order("id asc,weight asc").Find(&list)

	return util.ToPageInfo(count, int(count), 1, list), db.Error
}

func (r *RouterRepository) SelectById(id int) (model.Router, error) {
	var router model.Router
	err := global.DB.Take(&router, id).Error
	return router, err
}

func (r *RouterRepository) SelectByIds(ids []int) ([]model.Router, error) {
	var list []model.Router
	err := global.DB.Where("id in(?)", ids).Find(&list).Error
	return list, err
}
